import { IGame } from "./IGame";
import { LogicTileEntity } from "../LogicTileEntity";
import { GameManager } from "../GameManager";
import {TileMatrix, TileBlock, LinkedList, TileType} from "../Game/TileMatrix";
import { L2VMessageEnum } from "../../Framework/Network/L2VMessageEnum";
import {InputMessageEnum} from "../../Framework/Network/InputMessageEnum"


export class SanguoxiaoGame implements IGame {

    gameManager: GameManager;
    tiles: LogicTileEntity[][] = [];
    tileMatrix:TileMatrix = null;
    
    init(gameManager: GameManager, ...params: any[]): void
    {
        this.gameManager = gameManager;
        //Current 8x8 matrix, value:0~5
        this.tileMatrix = new TileMatrix(8,7,TileType.SPACE, gameManager);
    }

    tick(dt:number, input: any): void
    {
        if(input == null)
           return;
        //console.log("x: ", input.coord.x, " y:", input.coord.y);
        let cmd:InputMessageEnum = input.operation;
        console.log("rcv cmd:", cmd);
        switch(cmd){
            case InputMessageEnum.LongPress:
            {
                console.log("LongPress x: ", input.coord.x, " y:", input.coord.y);
                this.onCmdLongPress(input.coord.x,input.coord.y);
            }
            break;
            case InputMessageEnum.ShortPress:
            {
                console.log("ShortPress x: ", input.coord.x, " y:", input.coord.y);
                this.onCmdShortPress(input.coord.x,input.coord.y);
            }
            break;
            case InputMessageEnum.Up:
            {
                console.log("InputMessageEnum.UP");
                this.onCmdUp();
            }
            break;
        }
    }

    onDestroy(): void
    {
        this.gameManager = null;
    }

    //长按，堆叠块
    onCmdLongPress(x,y:number):void{
        console.log("prepare to process longPress:",x,y);
        this.tileMatrix.uiPile(x,y);
    }

    //消除连通块，如果没有连通块，则删除当前块
    onCmdShortPress(x,y:number):void{
        console.log("onCmdShortPress:",x,y);
        let tileBlock:TileBlock = this.tileMatrix.getElement(x,y);
        console.log("prepare to do:", tileBlock);
        if(tileBlock != null){
            // let stack:LinkedList<TileBlock> = new LinkedList<TileBlock>();
            // this.tileMatrix.search(x,y,tileBlock.value,stack);
            let stack:LinkedList<TileBlock> = this.tileMatrix.uiSearch(x,y);
            console.log("stack:", stack);
            let bbCnt = stack.size;
            console.log("connected block:", bbCnt);
            if(bbCnt == 1 || bbCnt == 2){
                //delete
                let delTileEntity = this.tileMatrix.uiDeleteTile(x,y);
            }else if(bbCnt >=3){
                //消除所有连通
                let tileBB:TileBlock = stack.pop();
                while(tileBB != null){
                    console.log("prepare to delete:", tileBB.x, tileBB.y);
                    this.tileMatrix.uiDeleteTile(tileBB.x, tileBB.y);
                    tileBB = stack.pop();
                }
            }
            this.tileMatrix.uiAdjustBlocks();
            this.tileMatrix.uiFillBlanks(true);
        }else{
            console.log("block not found on:",x,y);
        }
    }

    //向上升起一行
    onCmdUp():void{
        this.tileMatrix.uiUpLevel();
    }

    
}
